import { CreateGranaryRequest, Granary, ListGranariesRequest, ListGranariesResponse } from "@/types/proto/api/v1/granary_service";
import { DeleteGranaryRequest, UpdateGranaryRequest, GetGranaryRequest, RagTestRequest } from "@/types/proto/api/v1/granary_service";
import { RagTestResponse, ListGranaryResourcesRequest, GranaryStatus } from "@/types/proto/api/v1/granary_service";
import { CtrlGranaryRequest, CtrlGranaryResponse, ListGranariesTopicsRequest, ListGranariesTopicsResponse,ListGranaryResourcesResponse } from "@/types/proto/api/v1/granary_service";

import { granaryServiceClient} from "./grpcweb_auth";
import { Empty } from "@/types/proto/google/protobuf/empty";

export async function CreateGranary(data: CreateGranaryRequest): Promise<Granary> {
  try {
    return granaryServiceClient.createGranary(data)
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function UpdateGranary(data: UpdateGranaryRequest): Promise<Granary> {  
  try {
    return granaryServiceClient.updateGranary(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListGranaries(data: ListGranariesRequest): Promise<ListGranariesResponse> {
  try {
    return granaryServiceClient.listGranaries(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function CtrlGranary(data:CtrlGranaryRequest): Promise<CtrlGranaryResponse> {
  try {
    return granaryServiceClient.ctrlGranary(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function DeleteGranary(data: DeleteGranaryRequest): Promise<Empty> {
  try {
    return granaryServiceClient.deleteGranary(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function GetGranary(data: GetGranaryRequest): Promise<Granary> {
  try {
    return granaryServiceClient.getGranary(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListGranarisTopics(data: ListGranariesTopicsRequest): Promise<ListGranariesTopicsResponse> {
  try {
    return granaryServiceClient.listGranariesTopics(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function RagTest(data: RagTestRequest): Promise<RagTestResponse> {
  try {
    return granaryServiceClient.ragTest(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListGranaryResources(data: ListGranaryResourcesRequest): Promise<ListGranaryResourcesResponse>  {
  try {
    return granaryServiceClient.listGranaryResources(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export const formatGranaryStatus = (status: GranaryStatus | undefined) => {
  if (status == null) {
    return { text: '未知', type: 'info' }
  }
  
  switch (status) {
      case GranaryStatus.CREATED:
        return { text: '待解析', type: 'info' }
      case GranaryStatus.STARTING:
        return { text: '启动中', type: 'warning' }
      case GranaryStatus.ENDED:
        return { text: '构建成功', type: 'success' }
      case GranaryStatus.FAILED:
        return { text: '构建失败', type: 'danger' }
      case GranaryStatus.GRAPHING:
        return { text: '构建知识图谱', type: 'warning' }
      case GranaryStatus.CHUNKING:
        return { text: '分块', type: 'warning' }
      case GranaryStatus.SUMMARIZING:
        return { text: '总结', type: 'warning' }
      case GranaryStatus.STOPPED:
        return { text: '已停止', type: 'warning' }
      default:
        return { text: status.toString(), type: 'warning' }        
    }
}